package com.colorsstudio.defence.test
{
	import de.polygonal.ds.TreeBuilder;
	import idv.cjcat.fenofsm.events.StateEvent;
	import idv.cjcat.fenofsm.events.TransitionEvent;
	import idv.cjcat.fenofsm.events.*;
	import idv.cjcat.fenofsm.FSMachine;
	import idv.cjcat.fenofsm.State;
	import idv.cjcat.fenofsm.Transition;
	
	/**
	 * ...
	 * @author wuzhida
	 */
	public class Fsm
	{
		private var fsm:FSMachine;
		
		public function Fsm()
		{
			fsm = new FSMachine("s0");
			var s0:State = fsm.initState;
			var s1:State = fsm.createState("s1");
			var s2:State = fsm.createState("s2");
			var sGameEnd:State = fsm.createState("sGameEnd");
			var sGameEnd1:State = fsm.createState("sGameEnd1");
			
			var t01:Transition = fsm.createTransition(s0, s1, "forward", "t01", 0);
			var t12:Transition = fsm.createTransition(s1, s2, "forward", "t12", 0);
			var t20:Transition = fsm.createTransition(s2, s0, "forward", "t20", 0);
			var t02:Transition = fsm.createTransition(s0, s2, "backward", "t02", 0);
			var t21:Transition = fsm.createTransition(s2, s1, "backward", "t21", 0);
			var t10:Transition = fsm.createTransition(s1, s0, "backward", "t10", 0);
			var tToGameEnd:Transition = fsm.createTransition(s0, sGameEnd, "toEnd", "tToGameEnd", 0);
			var tToGameEnd1:Transition = fsm.createTransition(sGameEnd, sGameEnd1, "toEnd1", "tToGameEnd1", 0);
			
			s0.addEventListener(StateEvent.ENTER, stateListener);
			s0.addEventListener(StateEvent.EXIT, stateListener);
			s1.addEventListener(StateEvent.ENTER, stateListener);
			s1.addEventListener(StateEvent.EXIT, stateListener);
			s2.addEventListener(StateEvent.ENTER, stateListener);
			s2.addEventListener(StateEvent.EXIT, stateListener);
			sGameEnd.addEventListener(StateEvent.ENTER, gameOver);
			sGameEnd1.addEventListener(StateEvent.ENTER, gameOver);
			
			t01.addEventListener(TransitionEvent.INIT, transitionListener);
			t01.addEventListener(TransitionEvent.COMPLETE, transitionListener);
			t12.addEventListener(TransitionEvent.INIT, transitionListener);
			t12.addEventListener(TransitionEvent.COMPLETE, transitionListener);
			t20.addEventListener(TransitionEvent.INIT, transitionListener);
			t20.addEventListener(TransitionEvent.COMPLETE, transitionListener);
			t02.addEventListener(TransitionEvent.INIT, transitionListener);
			t02.addEventListener(TransitionEvent.COMPLETE, transitionListener);
			t21.addEventListener(TransitionEvent.INIT, transitionListener);
			t21.addEventListener(TransitionEvent.COMPLETE, transitionListener);
			t10.addEventListener(TransitionEvent.INIT, transitionListener);
			t10.addEventListener(TransitionEvent.COMPLETE, transitionListener);
			
			startTest();
		}
		
		private function gameOver(e:StateEvent):void
		{
			trace("The game is over!");
		}
		
		private function startTest():void
		{
			//note that the finite state machine is initially in the "s0" state  
			trace("===============");
			//triggers the transition "t01" from state "s0" to state "s1"
			fsm.input("forward");
			trace("===============");
			//triggers the transition "t12" from state "s1" to state "s2" 
			fsm.input("forward");
			trace("===============");
			//triggers the transition "t20" from state "s2" to state "s0"
			fsm.input("forward");
			trace("===============");
			//triggers the transition "t02" from state "s0" to state "s2"
			fsm.input("backward");
			trace("===============");
			//triggers the transition "t21" from state "s2" to state "s1" 
			fsm.input("backward");
			trace("===============");
			//triggers the transition "t10" from state "s1" to state "s0" 
			fsm.input("backward");
			trace("===============");
			//from s0 to the state gameEnd
			fsm.input("toEnd");
			trace("Is the game End?");
			//can it go from gameEnd to gameEnd1?
			fsm.input("toEnd1");
			trace("Is the road accessble");
		}
		
		private function transitionListener(e:TransitionEvent):void
		{
			switch (e.type)
			{
				case TransitionEvent.INIT: 
					trace("Transition started: " + e.transition.name);
					break;
				case TransitionEvent.COMPLETE: 
					trace("Transition completed: " + e.transition.name);
					break;
			}
		}
		
		private function stateListener(e:StateEvent):void
		{
			switch (e.type)
			{
				case StateEvent.ENTER: 
					trace("enterd! " + e.state.name);
					break;
				case StateEvent.EXIT: 
					trace("Exit! " + e.state.name);
					break;
			}
		}
	
	}

}